<?php

    function check_blacklist($url) 
    {
        $blacklist_extensions = array ('jpg', 'jpeg', 'gif', 'png', '.js' , '.us');
        $blacklist_domains = array ('imageshack.us', 'imagebanana.com', 'club-nation.eu', 'clubbers.pl');
        $blacklist_filenames = array ('...');

        if(in_string($url, $blacklist_extensions, 0, 1))
            return 0;

        if(in_string($url, $blacklist_domains))
            return 0;

        if(in_string($url, $blacklist_filenames))
            return 0;

        return 1;
    }
    
    function in_string($string, $array, $strict_prefix = 0, $strict_suffix = 0) 
    {
        foreach($array as $value) {
            if(preg_match("/".($strict_prefix ? "^" : ""). preg_quote($value) .($strict_suffix ? "$" : "")."/", $string))
                return 1;
        }
        return 0;
    }

    function delete_empty_threads($array)
    {
        $tmp_array = array();
 
        for($i=0; $i < sizeof($array); $i++) {

            $size = sizeof($array[$i]['thread_dl']);
            for($j=0; $j < $size; $j++){
                if(empty($array[$i]['thread_dl'][$j]['urls'])){
                    unset($array[$i]['thread_dl'][$j]);
                }
            }
            if(!empty($array[$i]['thread_dl']))
                $tmp_array[] = $array[$i];
        }
        return $tmp_array;
    }

    function get_new_data($release_data, $only_links)
    {
        $link = '';
        $links = array();

        $init_number_links = 0;

        for($j=0; $j < sizeof($release_data); $j++)
        {
            if(isset($release_data[$j]['thread_dl']))
            {
                for($i=0; $i < sizeof($release_data[$j]['thread_dl']); $i++)
                {
                    for($h=0; $h < sizeof($release_data[$j]['thread_dl'][$i]['urls']); $h++)
                    {
                        $link = $release_data[$j]['thread_dl'][$i]['urls'][$h] . "<br>";

                        $query = "SELECT COUNT(*) AS bekannt FROM crawler_releases WHERE release_url = '". $link ."';";
                        $mysql_connection->query($query);
                        $row = $mysql_connection->fetch();

                        if(!$row['bekannt'])
                        {
                            if($links_only)
                            {
                                $links[] = $link;
                            }
                            else
                            {
                                $links[$init_number_links]['source'] = $current_site;
                                $links[$init_number_links]['thread_url'] = $release_data[$j]['thread_url'];
                                $links[$init_number_links]['post_title'] = $release_data[$j]['thread_dl'][$i]['title'];
                                $links[$init_number_links]['thread_dl'][$i]['urls'][$h] = $link;
                                $links[$init_number_links]['thread_title'] = $release_data[$j]['thread_title'];
                                $links[$init_number_links]['thread_post_date'] = $release_data[$j]['thread_post_date'];

                                $query = "INSERRT INTO crawler_releases (release_id, release_source, release_thread_url, release_post_title, release_download_url, release_thread_title, release_thread_date) VALUES ('', '".$current_site."', '".$release_data[$j]['thread_url']."', '".$release_data[$j]['thread_dl'][$i]['title']."', '".$link."', '".$release_data[$j]['thread_title']."', '".$release_data[$j]['thread_post_date']."');";
                                $mysql_connection->query($query);

                                $init_number_links++;
                            }
                        }                        
                    }
                }
            }
        }

        return $links;
    }
?>